﻿﻿using System;
 using UnityEngine;
using UnityEngine.EventSystems;

namespace Script.EnemyBehavior
{
    public class JumpAttackBehavior : StateBehavior
    {
        private GameObject target;
        private Rigidbody2D rb;

        [SerializeField] private float jumpSpeed = 10;

        private Vector2 jumpPos;
        
        private static readonly int JumpAttack = Animator.StringToHash("JumpAttack");

        private void Awake()
        {
            target = GameObject.FindWithTag("Player");
        }

        public override void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo,
            int layerIndex)
        {
            rb = animator.GetComponent<Rigidbody2D>();
            jumpPos = target.transform.position;
        }

        public override void OnStateExit(Animator animator, AnimatorStateInfo stateInfo,
            int layerIndex)
        {
        }

        public override void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo,
            int layerIndex)
        {
            if (Vector3.Distance(rb.position, jumpPos) < 1.2f)
            {
                rb.velocity = Vector2.zero;
                
                animator.SetBool(JumpAttack, false);
            }
            else
            {
                rb.velocity = (jumpPos - rb.position).normalized * jumpSpeed;
            }

        }
    }
}